const ReaderDao = require("../../dao/reader/reader_dao.js");

const ReaderServices = {
	//添加职位
	add(req, res, next) {
		//获取post请求中的数据
		//上传文件的logo名
		let logo = "";
		if(req.file) {
			logo = "/images/upload/" + req.file.filename;
		}
		const {			
			readername,
			readerbookname,
			readerstatus
		} = req.body;
		//保存数据库
		ReaderDao.save({
				readername,	
				readerbookname,
				readerstatus
			})
			.then((data) => {
				res.json({
					res_code: 1,
					res_error: "",
					res_body: {
						status: 1,
						data: data
					}
				});
			})
			.catch((data) => {
				res.json({
					res_code: 0,
					res_error: err,
					res_body: {}
				});
			});
	},
	findByPage(req, res, next) {
		//获取查询页码
		const {
			page
		} = req.query;
		//查询指定页码数据
		ReaderDao.findByPage(page)
			.then((data) => {
				res.json({
					res_code: 1,
					res_error: "",
					res_body: {
						status: 1,
						list: data
					}
				});
			})
			.catch((err) => {
				res.json({
					res_code: 0,
					res_error: err,
					res_body: {}
				});
			});
	},

	deleteById(req, res, next) {
		const {
			id
		} = req.query;
		ReaderDao.deleteById(id)
			.then((data) => {
				res.json({
					res_code: 1,
					res_error: "",
					res_body: {}
				});
			})
			.catch((err) => {
				res.json({
					res_code: 0,
					res_error: err,
					res_body: {}
				});
			});
	},

	updateById(req, res, next) {
		console.log(req.body);
		let logo = "";
		if(req.file) {
			logo = "/images/upload/" + req.file.filename;
		}
		const {
			readerid,
			readername,
			readerbookname,
			readerstatus
		} = req.body;
		ReaderDao.updateById({
				readerid,
				readername,
				readerbookname,
				readerstatus
			})
			.then((data) => {
				console.log(data);
				res.json({
					res_code: 1,
					res_error: "",
					res_body: {
						status: 1
					}
				});
			})
			.catch((err) => {
				res.json({
					res_code: 0,
					res_error: err,
					res_body: {
						status: 0
					}
				});
			});
	}
};
module.exports = ReaderServices;